#!/bin/bash
set -e

function info() {
echo Usage: `basename $0` in.vcf cancer.bed
exit 65
}

while getopts  ":p:" opts
do
        case  $opts  in
		p) out_prefix=$OPTARG;;
		*) info;;
        esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi


. /mnt/ilustre/app/medical/tools/.var

echo;echo;echo gatk SelectVariants snp
java $tmp -Xmx$java_memory -jar $gatk \
-T  SelectVariants \
-R $ref_genome \
-V $1 \
-o $out_prefix.snp.vcf \
-selectType SNP


echo;echo;echo gatk SelectVariants indel
java $tmp -Xmx$java_memory -jar $gatk \
-T  SelectVariants \
-R $ref_genome \
-V $1 \
-o $out_prefix.indel.vcf \
-selectType INDEL



echo; echo filter out mutations that occurred more than once within a 10-bp window
filter_clustered_mutations.pl $out_prefix.indel.vcf $out_prefix.snp.vcf

echo; echo Filter out detected mutations on the basis of allelic frequency, and add the passcode string to the end of the VCF4 lines
add_passcode_2_filtered_vcf.10andVaried_6and2forD.IPCT.pl $out_prefix.snp.vcf.cluster_filtered.vcf $out_prefix.snp.vcf.cluster_filtered.passcode.vcf $2


# Use the ‘grep’ command and Perl one-liners to extract interested mutations. For example, assuming that the last sample is the normal sample, the command to grab all somatic mutations that are detected in more than one cell is as follows:
grep "<E.….0>" $out_prefix.snp.passcode.vcf | 
perl -e
'chomp; @b = split(/\t/ ); @a = split(//, $b[-1]); $num_cell=0; $size=@a; for($i=2; $i<$size;$i++){if($a[$i] eq "1" || $a[$i] eq "2" ){$num_cell++;}} if($num_cell > 1){print STDERR
"$_\n";'
